﻿<!DOCTYPE html>
<head>
    <meta charset="UTF-8">

    <script type="text/javascript">

        const url = "ws://localhost:8081/"
        var pc, ws;

        async function start() {

            closePeer();

            let audioControl = document.querySelector('#audioCtl');

            //const constraints = {
            //    'video': true,
            //    'audio': true
            //}

            const localStream = await navigator.mediaDevices.getUserMedia({ video:false, audio: true });

            ws = new WebSocket(url, []);

            ws.onopen = function () {
                console.log("web socket onopen.");

                pc = new RTCPeerConnection(null);

                localStream.getTracks().forEach(track => {
                    console.log('add local track ' + track.kind + ' to peer connection.');
                    console.log(track);
                    pc.addTrack(track, localStream);
                });


                pc.onicegatheringstatechange = function () {
                    console.log("onicegatheringstatechange: " + pc.iceGatheringState);
                }

                pc.oniceconnectionstatechange = function () {
                    console.log("oniceconnectionstatechange: " + pc.iceConnectionState);
                }

                pc.onsignalingstatechange = function () {
                    console.log("onsignalingstatechange: " + pc.signalingState);
                }

                pc.onicecandidate = function (event) {
                    if (event.candidate) {
                        console.log('new-ice-candidate:');
                        console.log(event.candidate.candidate);
                        console.log(event.candidate);
                        //console.log(pc.localDescription.sdp);
                        ws.send(event.candidate.candidate);
                    }
                };
            };

            ws.onmessage = async function (evt) {
                // Received SDP offer from the remote web socket server.
                console.log("web socket onmessage: " + evt.data + ".");
                await pc.setRemoteDescription(new RTCSessionDescription({ type: "offer", sdp: evt.data }))

                // Now create our offer SDP to send back to the web socket server.
                pc.createAnswer().then(function (answer) {
                    return pc.setLocalDescription(answer);
                }).then(function () {
                    console.log("Sending answer SDP.");
                    console.log("SDP: " + pc.localDescription.sdp);
                    ws.send(pc.localDescription.sdp);
                });
            };
        };

        function closePeer() {
            //let stm = document.querySelector('#videoCtl').srcObject;
            //if (stm != null) {

            //    stm.getTracks().forEach((trk) => {
            //        console.log(`stopping track ${trk.id} kind ${trk.kind} readyState ${trk.readyState}.`);
            //        console.log(trk);
            //        trk.stop();
            //    });
            //}

            if (ws != null) {
                console.log("closing web socket.");
                ws.close();
            }

            if (pc != null) {
                console.log("close peer");
                pc.close();
            }
        };

    </script>
</head>
<body>

    <div>
        <button type="button" class="btn btn-success" onclick="start();">Start</button>
        <button type="button" class="btn btn-success" onclick="closePeer();">Close</button>
    </div>

</body>
